package com.miyake.dsp.otdr;

import java.util.HashMap;
import java.util.Map;

public class OtdrSettingTable {
	public class Setting {
		public double getPulseWidth() {
			return pulseWidth;
		}
		public void setPulseWidth(double pulseWidth) {
			this.pulseWidth = pulseWidth;
		}
		public double getAttenuator() {
			return attenuator;
		}
		public void setAttenuator(double attenuator) {
			this.attenuator = attenuator;
		}
		private double pulseWidth;
		private double attenuator;
		public Setting(double pulseWidth, double atteuator) {
			this.pulseWidth = pulseWidth;
			this.attenuator = atteuator;
		}
	};
	
	private double[] distanceRaneTable = {0.5, 1.0, 2.5, 5.0, 10.0, 25.0, 50.0, 100.0, 200.0};
	private double[] pulseWidthTable = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000};
	private double[] attenuatorTable = {0, 3, 6, 9, 12, 15, 18, 21};

	
	private Map<Double, Double> bandwidthMap = new HashMap<Double, Double>(); // <Attenuator, Bandwidth>
	private Map<Double, Setting> defaultSettingMap = new HashMap<Double, Setting>(); // <Distance Range, Setting>
	

	
	public OtdrSettingTable() {
		bandwidthMap.put(0.0, 5e7);
		bandwidthMap.put(3.0, 2e6);
		bandwidthMap.put(6.0, 3e6);
		bandwidthMap.put(9.0, 4e6);
		bandwidthMap.put(12.0, 5e6);
		bandwidthMap.put(15.0, 6e6);
		bandwidthMap.put(18.0, 7e6);
		bandwidthMap.put(21.0, 8e6);
		
		defaultSettingMap.put(0.5, new Setting(5.0, 0.0));
		defaultSettingMap.put(1.0, new Setting(10.0, 0.0));
		defaultSettingMap.put(2.5, new Setting(20.0, 0.0));
		defaultSettingMap.put(5.0, new Setting(50.0, 0.0));
		defaultSettingMap.put(10.0, new Setting(50.0, 0.0));
		defaultSettingMap.put(25.0, new Setting(50.0, 0.0));
		defaultSettingMap.put(50.0, new Setting(100.0, 0.0));
		defaultSettingMap.put(100.0, new Setting(500.0, 0.0));
		defaultSettingMap.put(200.0, new Setting(20000.0, 0.0));
	}
	
	public Setting getDefaultSetting(double distanceRange) {
		return defaultSettingMap.get(distanceRange);
	}
	
	public double getBandwidth(double attenuator) {
		return bandwidthMap.get(attenuator);
	}
	
	public double getRange(double noiseStart) {
		for (int i = 0; i < distanceRaneTable.length; i++) {
			if (distanceRaneTable[i] > noiseStart) {
				return distanceRaneTable[i+1];
			}
		}
		return distanceRaneTable[distanceRaneTable.length - 1];
	}
}
